package EsManager

import (
	esmanagerDao "deepgox-go-admin/Dao/EsManager"
	managerDao "deepgox-go-admin/Dao/Manager"
	esmanagerModel "deepgox-go-admin/Models/EsManager"
	managerModels "deepgox-go-admin/Models/Manager"
	"deepgox-go-admin/Pkg/HttpResponse"
	"github.com/gin-gonic/gin"
	"strconv"
)

// ByIdEsManagerHandle 查询到的管理信息写入Es
//
//	@Summary		查询到的管理信息写入Es
//	@Description	查询到的管理信息写入Es
//	@Tags			查询到的管理信息写入Es
//	@Accept			application/json
//	@Produce		application/json
//	@Param			req	body	managerModels.ByIdModelHandler	true	"req"
//	@type:			managerModels.ByIdModelHandler
//	@Success		200	{object}	Response._ResponsePostList
//	@Router			/v1/manager/byides [post]
func ByIdEsManagerHandle(c *gin.Context) {
	p := new(managerModels.ByIdModelHandler)
	if err := c.ShouldBindJSON(&p); err != nil {
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数格式错误")
		return
	}
	if len(strconv.FormatInt(p.Id, 36)) == 0 {
		//请求参数有误
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数不能为空")
		return
	}

	//查询出所对应的数据
	Manager := &managerDao.ByIdModelHandler{managerModels.ByIdModelHandler{Id: p.Id}}
	var result, err = Manager.ByIdManager()
	if err != nil {
		HttpResponse.ResponseError(c, HttpResponse.CodeServerBusy)
		return
	}
	//判断索引是否存在
	esManager := &esmanagerDao.EsManagerModelDoc{}
	err = esManager.CreateIndexIfNotExists()
	if err == nil {
		//向索引写入数据
		esByidManager := &esmanagerDao.EsManagerByIdModeHandler{esmanagerModel.EsManagerByIdModel{ID: strconv.FormatInt(p.Id, 10)}}
		doc, err := esByidManager.CreateDoc(strconv.FormatInt(p.Id, 10), result)
		if err != nil {
			HttpResponse.ResponseError(c, HttpResponse.CodeServerBusy)
			return
		}
		HttpResponse.ResponseSuccess(c, doc)
	}
	HttpResponse.ResponseSuccess(c, result)
}

// ByIdEsManagerDataHandle 查询ES管理者信息
//
//	@Summary		查询ES管理者信息
//	@Description	查询ES管理者信息
//	@Tags			查询ES管理者信息
//	@Accept			application/json
//	@Produce		application/json
//	@Param			req	body	managerModels.ByIdModelHandler	true	"req"
//	@type:			managerModels.ByIdModelHandler
//	@Success		200	{object}	Response._ResponsePostList
//	@Router			/v1/manager/getesmanagerinfo [post]
func ByIdEsManagerDataHandle(c *gin.Context) {
	p := new(managerModels.ByIdModelHandler)
	if err := c.ShouldBindJSON(&p); err != nil {
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数格式错误")
		return
	}
	if len(strconv.FormatInt(p.Id, 36)) == 0 {
		//请求参数有误
		HttpResponse.ResponseErrorWithMsg(c, HttpResponse.CodeInvalidParams, "参数不能为空")
		return
	}
	EsByidManagerinfo := &esmanagerDao.EsManagerByIdModeHandler{esmanagerModel.EsManagerByIdModel{ID: strconv.FormatInt(p.Id, 10)}}
	doc, err := EsByidManagerinfo.FirstDoc(strconv.FormatInt(p.Id, 10))
	if err != nil {
		HttpResponse.ResponseError(c, HttpResponse.CodeServerBusy)
		return
	}
	HttpResponse.ResponseSuccess(c, doc)
}
